<?php
class FurmiHelper extends Helper {

	var $helpers = Array("Html","Ajax",'Error');

	/**
	 * $field[0] - type - string
	 * $field[1] - html attrib - array
	 * $field[2] - id,name - string
	 * $field[3] - nazwa - string
	 * $field[4] - 0,null - normal, 1 - w/o - ajax - int, 2 - w/o row 2, 3- HELP
	 * $field[5] - 1 null, 0 required
	 * $field[6] - array($elements,$selected,$oa,$sel_null)
	 */
	//a('file',aa('size','27'),'Company/logo','Plik loga',1,1)

	function createForm($fields)
	{
		foreach ($fields as $field) {

			// set model/field/controller data // # $mo $fi $co
			$da = explode('/',$field[2]);
			$mo = $da[0];
			$fi = $da[1];
			$co = Inflector::pluralize($mo);


			// set new id # $fid
			$fid = $mo.'_'.$fi;

			// get html attributes // #ha
			if(is_array($field[1]))
			{
				$ha = array_merge($field[1],aa('id',$fid,'class','f_'.$field[0]));
			}
			else
			{
				$ha = aa('id',$fid,'class','f_'.$field[0]);
			}

			// -------- set label tag # $label
			if($field[5] == 1)
			{
				$label = '<label for="'.$fid.'">'.$field[3].'</label>';
			}
			else // required
			{
				$label = '<label for="'.$fid.'">'.$field[3].' <span class="required">*</span></label>';
			}

			// --------- set input type tag #input
			switch($field[0])
			{
				case 'input':
					$input = $this->Html->input($field[2],$ha);
					break;

				case 'textarea':
					$input = $this->Html->textarea($field[2],$ha);
					break;

				case 'checkbox':
					$input = $this->Html->checkbox($field[2],null,$ha);
					break;
				case 'selecttag1':
												//$fieldName, $optionElements, $selected = null, $selectAttr = array(), $optionAttr = null, $showEmpty = true, $return = false
					$input = $this->Html->selectTag($field[2],$field[6][0],$field[6][1],$ha,$field[6][2],$field[6][3]);
					break;					
					
				default:
					$input = $this->Html->$field[0]($field[2],$ha);
			}

			// --------- set ajax script observer # $observer
			$observer = $this->Ajax->observeField($fid,
			array(
			'update' => 'msg_'.$fid,
			'url' => '/'.$co.'/ajaxValidate/',
			'frequency' => '1'));

			$observerFake= $this->Ajax->observeField($fid,
			array(
			'update' => 'msg_'.$fid,
			'url' => '/'.$co.'/ajaxValidateFake/',
			'frequency' => '1'));
			
			$observerHelp= $this->Ajax->observeField($fid,
			array(
			'update' => 'msg_'.$fid,
			'url' => '/'.$co.'/ajaxValidateHelp/',
			'frequency' => '1'));

			// --------- set normal validate tag # $errMsg $msg
			$errMsg = $this->Error->messageFor($field[2]);
			$msg = '<span id="msg_'.$fid.'">'. $errMsg .' </span>';

			// === ROW 1 - label

			$row1 = '';
			//var_dump($field[5]);
			if($field[3]!=null)
			{
				$row1 = '<div class="f_label_'.$field[0].'">'.$label.'</div>';
			}

			//=== ROW 2 - input

			//$row2 = '<div class="f_input_'.$field[0].'"> '.$input.'</div>';
			$row2 = $input;

			//=== ROW 3 - err and ajax msg (only for ajax on, or then errors found)

			//if errors found
			if($errMsg !=null ) //AND $field[4] == 0) AND $field[4]!=2)
			{
				//if ajax on
				if($field[4] == 0)
				{
					$row3 = $observer.'<div class="tmp1">&nbsp;'.$msg.'</div>';
					$row2.=$row3;
				}
				//if ajax off
				if($field[4] == 1)
				{
					$row3 = $observerFake.'<div class="tmp1">&nbsp;'.$msg.'</div>';
					$row2.=$row3;
				}
			}
			else // if w/o errors
			{
				//if ajax on
				if($field[4] == 0)
				{
					$row3 = $observer.'<div class="tmp1">&nbsp;'.$msg.'</div>';
					$row2.=$row3;
				}

			}
			//help
			if($field[4] == 3)
			{
					$row3 = $observerHelp.'<div class="tmp1">&nbsp;'.$msg.'</div>';
					$row2.=$row3;
			}

			// !!! OUTPUT
			if($field[0]!='checkbox')
			{
				echo $row1.$row2;
			}
			else
			{
				echo $row2.$row1;
			}

		}


	}


	function MainMenu($menu)
	{
		$co= $this->params['controller'];
		$ac= $this->params['action'];

		$cl = '';
		
		$out = '<ul class="nav">';

		foreach ($menu as $item)
		{
			if(!isset($item[4]))
			{
				$item[4]='inactive';
			}elseif($item[4]=='employer')
			{
				$cl = 'style="float:right;"';
			}
			
			if(($item[2] == $co && in_array($ac,$item[3])) )// or ($item[2] == $co && $item[3] == 'index'))
			{
				$out.= '<li class="active" '.$cl. '>';
				//$out.= ' style="padding:3px 2px 0 0;" >';
			}
			else
			{
				$out.= '<li class="'.$item[4].'"';
				//$out.= ' style="padding:3px 2px 0 0;" ';
				//$out.=' onmouseover="RoundedTopNew(this,\'transparent\',\'#FF0084\');  " ';
				//$out.=' onmouseout="RoundedTopNew(this,\'transparent\',\'#4096EE\');" ';
				$out.= '>';
				//$out.=' onmouseover="RoundedTopNew(this,\'transparent\',\'#FF0084\');  " ';
				//$out.=' onmouseout="RoundedTopNew(this,\'transparent\',\'#4096EE\');" >';
				//$out.=' onmouseover="Effect.Appear(this);"> ';
			}
			$out.= $this->Html->link($item[0],$item[1]);
			$out.= '</li>';
		}

		return $out.'</ul>';
	}
}
?>
